<?php
namespace app\admin\controller;

use \think\Controller;
use \think\Request as Request;
use app\admin\model\User as UserModel;
class User extends Base
{
    /**
     * 显示所有用户
     */
    public function index()
    {
        $model = new UserModel;
        $field = ['u_id','u_name','u_status','last_login_ip','last_login_time'];
        $data = $model->field($field)->select();
        if(empty($data)){
            $this->err();
        }else{
            $this->assign('page_title','用户列表');
            $this->assign('data',$data);
            return $this->fetch();
        }
    }

    public function add(){
        $this->assign('name','添加用户');
        return $this->fetch();
    }
    
    //添加用户方法
    public function insert(){
        $re = Request::instance();
        $name       = $re->post("username",'admin,user');
        $password   = $re->post("password");
        $auth_group = $re->post("auth_group");
        if(empty($name)){
            return $this->fetch('public/error','你没有输入用户名');
        }
        if(empty($password)){
            return $this->fetch('public/error','你没有输入密码');
        }
        if(empty($auth_group)){
            return $this->fetch('public/error','你没有输入用户组');
        }
        $up_time    = $create_time = time();
        $model      = new UserModel;
        $password   = password_hash($password,PASSWORD_DEFAULT);
        $data = [
            'u_name'      => $name,
            'u_password'  => $password,
            'create_time' => $create_time,
            'up_time'     => $up_time,
        ];

        $result = $model->save($data);
        if($result){
            return $this->fetch('public/success');
        }else{
            return $this->fetch('public/error');
        }
    }
}
